package com.smarthospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.smarthospital.common.Result.CodeMsg;
import com.smarthospital.common.Result.CommonException;
import com.smarthospital.contants.SHConst;
import com.smarthospital.dao.daoService.DoctorDaoService;
import com.smarthospital.dao.daoService.InquiryOrderDaoService;
import com.smarthospital.dao.daoService.UserDaoService;
import com.smarthospital.entity.pojo.Doctor;
import com.smarthospital.entity.pojo.InquiryOrder;
import com.smarthospital.entity.pojo.User;
import com.smarthospital.service.OrderService;
import com.smarthospital.service.TRTCApiService;
import com.smarthospital.web.inteceptor.LoginInterceptor;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * create by sym  2021/5/29 17:33.
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private InquiryOrderDaoService inquiryOrderDaoService;

    @Autowired
    private UserDaoService userDaoService;

    @Autowired
    private TRTCApiService trtcApiService;

    @Override
    public PageInfo<InquiryOrder> querMyInquiryOrder(Integer doctorLevel, Integer pageSize, Integer pageNum) {
        User user = LoginInterceptor.getUser();
        PageInfo<InquiryOrder> pageInfo = inquiryOrderDaoService.querMyInquiryOrder(user.getId(), doctorLevel, pageSize, pageNum);
        List<InquiryOrder> list = pageInfo.getList();
        list.sort(Comparator.comparing(InquiryOrder::getUpdateTime).reversed());
        return pageInfo;
    }

    @Override
    public InquiryOrder getOrder(Long id) {
        InquiryOrder order = inquiryOrderDaoService.getOrderById(id);
        User user = userDaoService.selectById(order.getInquiryManId());
        Doctor doctor = doctorDaoService.queryById(order.getDoctorId());
        if (null != user) {
            order.setPicture(user.getPicture());

        }
        if (null != doctor) {
            order.setDocPicture(doctor.getAvatar());
        }
        return order;
    }

    @Autowired
    private DoctorDaoService doctorDaoService;
    /**
     * $.$  提交订单
     * @param order
     */
    @Override
    public void addOrder(InquiryOrder order) {
        //只能有一份待付款订单
        User user = LoginInterceptor.getUser();
        InquiryOrder buider = InquiryOrder.builder()
                .inquiryManId(user.getId())
                .status(SHConst.order_status.STATUS_0)
                .build();
        List<InquiryOrder> list = inquiryOrderDaoService.queryOrder(buider);
        if (list.size()>0) {
            throw new CommonException(CodeMsg.ERROR_NO_PAY_ORDERS);
        }
        //插入数据
        //计算金额
        Doctor doctor = doctorDaoService.queryById(order.getDoctorId());
        double total = order.getInquiryType()==0? doctor.getImageTextPrice():order.getInquiryType()==1?doctor.getPhonePrice():doctor.getVideoPrice();
        int totalCoins = Integer.valueOf((total*100)+"") ;
        order.setTotal(totalCoins);
        inquiryOrderDaoService.insertSelective(order);
    }



    @Override
    public void updateOrder(InquiryOrder order) {
        //修改数据
        inquiryOrderDaoService.update(order);
    }

    /**
     * ^_^ 完成订单  在聊天场景调用
     * 关闭聊天房间
     * @param orderId
     */
    @Override
    public InquiryOrder complateOrder(Long orderId) throws TencentCloudSDKException {

        InquiryOrder thisOder = inquiryOrderDaoService.queryById(orderId);
        log.info("【关闭订单】 order {}", JSONObject.toJSONString(thisOder));
        if (1 != thisOder.getStatus()) {
            throw new CommonException(CodeMsg.ERROR_ORDER_STATUS);
        }
        InquiryOrder order = InquiryOrder.builder().id(orderId).status(3).build();
        inquiryOrderDaoService.update(order);
        log.info("【关闭订单】 完成 order {}", JSONObject.toJSONString(thisOder));
        thisOder.setStatus(3);
        if (thisOder.getInquiryType() == 2) {
            trtcApiService.closeTRTCRoom(thisOder.getRoomId());
            log.info("【关闭订单】 关闭腾讯音视频房间 order {}", JSONObject.toJSONString(thisOder));
        }
        return thisOder;
    }
}
